#!/usr/bin/env python3
# coding=utf-8


"""
@author: zhao
"""

import numpy as np
import cv2
import matplotlib.pyplot as plt

img = cv2.imread('data/Lena.bmp')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = np.array(img, dtype=np.float64)
g_x = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])
g_y = np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]])
laplace = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]])

img_g_x = cv2.filter2D(img, -1, g_x)
img_g_y = cv2.filter2D(img, -1, g_y)
img_laplace = cv2.filter2D(img, -1, laplace)

img_graid = np.sqrt(img_g_x ** 2 + img_g_y ** 2)
img_angle = np.arctan(img_g_y / (img_g_x + 2**-1000))

plt.subplot(2, 3, 1), plt.imshow(img, cmap='gray'), plt.title(
    'source'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 3, 2), plt.imshow(img_g_x, cmap='gray'), plt.title(
    'x gradient'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 3, 3), plt.imshow(img_g_y, cmap='gray'), plt.title(
    'y gradient'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 3, 4), plt.imshow(img_graid, cmap='gray'), plt.title(
    'gradient  amplitude'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 3, 5), plt.imshow(img_angle, cmap='gray'), plt.title(
    'angle'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 3, 6), plt.imshow(img_laplace, cmap='gray'), plt.title(
    'Laplace'), plt.xticks([]), plt.yticks([])
plt.show()